package cn.edu.scau.cmi.ema.controller;

import cn.edu.scau.cmi.ema.base.CmiPagination;
import cn.edu.scau.cmi.ema.base.CmiEmaController;
import cn.edu.scau.cmi.ema.domain.Event;
import cn.edu.scau.cmi.ema.domain.Food;
import cn.edu.scau.cmi.ema.domain.Picture;
import cn.edu.scau.cmi.ema.util.CmiHibernateProxyUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Set;

@Controller("PictureController")
public class PictureController extends CmiEmaController {

	@RequestMapping("/listPictures")
	public ModelAndView listPictures(HttpServletRequest request, @ModelAttribute CmiPagination pagination) {
		ModelAndView mav = new ModelAndView();

		pagination = picturePaginationService.updatePagination(request, pagination);
		Set<Picture> pictures = picturePaginationService.getPaginationPictures(pagination);

		mav.addObject("pictures", pictures);
		mav.addObject("entityName", "picture");
		mav.addObject("pagination", pagination);
		mav.setViewName("picture/listPictures");
		return mav;
	}

	public String listPictures() {
		return "redirect:/listPictures";
	}

	@RequestMapping("/viewPicture")
	public ModelAndView viewPicture(@RequestParam Integer pictureId) {
		ModelAndView mav = new ModelAndView();
		mav.addObject("picture", pictureDAO.findPictureByPrimaryKey(pictureId));
		mav.setViewName("picture/viewPicture");
		return mav;
	}

	@RequestMapping(value = "/confirmDeletePictures")
	public ModelAndView confirmDeletePictures(
			@RequestParam(value = "deletePicturesIds", required = true) Set<Integer> deletePicturesIds) {
		ModelAndView mav = new ModelAndView();

		Set<Picture> deletePictures = pictureDAO.findPicturesByIds(deletePicturesIds);
		Set<Picture> referencedPictures = pictureService.loadReferencePictures(deletePictures);
		boolean hasReferenced;
		if (referencedPictures.size() == 0) {
			mav.addObject("deleteEntitys", deletePictures);
			hasReferenced = false;
		} else {
			deletePictures.removeAll(referencedPictures);
			mav.addObject("referencedEntitys", referencedPictures);
			mav.addObject("deleteEntitys", deletePictures);
			hasReferenced = true;
		}
		mav.addObject("entityName", "Picture");
		mav.addObject("hasReferenced", hasReferenced);
		mav.setViewName("forward:/listPictures");
		return mav;
	}

	@RequestMapping("/deletePicture")
	public String deletePicture(@RequestParam Integer pictureId) {
		Picture picture = pictureDAO.findPictureByPrimaryKey(pictureId);
		pictureService.deletePicture(picture);
		return "forward:/listPictures";
	}

	@RequestMapping("/confirmDeletePicture")
	public ModelAndView confirmDeletePicture(@RequestParam Integer pictureId) {

		ModelAndView mav = new ModelAndView();

		mav.addObject("picture", pictureDAO.findPictureByPrimaryKey(pictureId));

		mav.setViewName("picture/deletePicture");

		return mav;
	}

	@RequestMapping("/deletePictures")
	public String deletePictures(
			@RequestParam(value = "deletePicturesId", required = true) Set<Integer> deletePicturesId) {
		pictureService.deletePictures(deletePicturesId);
		return "forward:/listPictures";
	}

	@RequestMapping("/editPicture")
	public ModelAndView editPicture(@RequestParam Integer pictureId) {

		ModelAndView mav = new ModelAndView();

		Picture picture = pictureDAO.findPictureByPrimaryKey(pictureId);
		// 将需要编辑的对象加到mav对象中
		mav.addObject("picture", picture);

		// 把所有的引用picture对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(pictureService.loadPictureObjectReferencedSet(picture));

		// 将编辑页面picture添加到mav中
		mav.setViewName("picture/editPicture");
		return mav;
	}

	@RequestMapping("/newPicture")
	public ModelAndView newPicture() {
		ModelAndView mav = new ModelAndView();
		Picture picture = new Picture();

		mav.addObject("picture", picture);

		// 把所有的引用picture对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(pictureService.loadPictureObjectReferencedSet(picture));

		mav.addObject("newFlag", true);
		mav.setViewName("picture/editPicture");

		return mav;
	}

	@RequestMapping("/savePicture")
	public ModelAndView savePicture(@ModelAttribute Picture picture) {

		ModelAndView mav = new ModelAndView();

		Event eventTemp = picture.getEvent();
		Event event = eventTemp == null ? null
				: CmiHibernateProxyUtil.deproxy(eventDAO.findEventById(eventTemp.getId()));
		Food foodTemp = picture.getFood();
		Food food = foodTemp == null ? null : CmiHibernateProxyUtil.deproxy(foodDAO.findFoodById(foodTemp.getId()));

		Picture existingPicture = CmiHibernateProxyUtil
				.deproxy(pictureDAO.findPictureByPhysicalPrimaryKey(picture.getName(), picture.getPath()));

		picture.setEvent(event);
		picture.setFood(food);
		// 新建记录
		if (picture.getId() == null) {
			if (picture.equals(existingPicture)) {

				mav.addAllObjects(pictureService.loadPictureClassReferencedSet(picture));
				// mav = editContinue(mav, picture);
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("picture/editPicture");
			} else {
				pictureService.savePicture(picture);
				mav.setViewName("forward:/listPictures");
			}
		}
		// 编辑记录
		else {
			Picture originPicture = CmiHibernateProxyUtil.deproxy(pictureDAO.findPictureByPrimaryKey(picture.getId()));
			if (!picture.equals(originPicture) && picture.equals(existingPicture)) {
				mav.addAllObjects(pictureService.loadPictureClassReferencedSet(picture));
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("picture/editPicture");
			} else {
				pictureService.savePicture(picture);
				mav.setViewName("forward:/listPictures");
			}
		}
		return mav;
	}

	@RequestMapping("/pictureController/binary.action")
	public ModelAndView streamBinary(@ModelAttribute HttpServletRequest request,
			@ModelAttribute HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("streamedBinaryContentView");
		return mav;

	}

}
